package com.easidea.tim.flex.remote.customer
{
	import com.easidea.tim.flex.Env;
	import com.easidea.tim.flex.bean.Page;

	import com.easidea.tim.flex.bean.customer.CustomerType;
	import com.easidea.tim.flex.event.CustomerEvent;
	import com.easidea.tim.flex.remote.BaseDao;
	import com.easidea.tim.flex.util.AppAlert;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.mxml.RemoteObject;
	
	import net.digitalprimates.persistence.hibernate.rpc.HibernateRemoteObject;
	
	
	
	public class CustomerTypeDao extends BaseDao
	{
		private var _customerTypeService:HibernateRemoteObject = new HibernateRemoteObject();
		private static var _instance:CustomerTypeDao;
		public static function getInstance():CustomerTypeDao{
			if( !_instance ){
				_instance = new CustomerTypeDao();
				
			}
			return _instance;
		}
		public function CustomerTypeDao() {

				_customerTypeService.channelSet = this.buildChannelSet();
				_customerTypeService.destination="customerTypeService";	
				_customerTypeService.concurrency = "last";
				_customerTypeService.makeObjectsBindable = true;		
				_customerTypeService.showBusyCursor = true;		
		}
		/**
		 * 调用spring方法:根据客户名称等参数分页查询客户列表
		 * @param name
		 * @param page
		 * 
		 */		
		public function getCustomerTypeList(name:String,page:Page):void {	
			
			_customerTypeService.addEventListener(ResultEvent.RESULT, showCustomerTypeList);
			_customerTypeService.addEventListener(FaultEvent.FAULT,_Fault);
			_customerTypeService.getCustomerTypeListByCondition(name,page);
		}

		/**
		 * 根据客户名称等参数分页查询客户列表回调函数
		 * @param e
		 * 
		 */			
		private function showCustomerTypeList(e:ResultEvent):void {		
			_customerTypeService.removeEventListener(ResultEvent.RESULT, showCustomerTypeList);
			 if(e.result != null) {					
				Env.dispatchEvent(new CustomerEvent("getCustomerTypeListSuccess",e.result));				  									 	
			 } 
		}

		/**
		 *调用spring方法:新增或编辑客户保存 
		 * @param customer
		 * 
		 */		
		public function saveCustomerType(customerType:CustomerType):void {
				_customerTypeService.addEventListener(FaultEvent.FAULT,_Fault);
				_customerTypeService.addEventListener(ResultEvent.RESULT, saveCustomerTypeSuccess);
				_customerTypeService.saveOrUpdateCustomerType(customerType);
		}
	   /**
	    *新增或编辑客户保存回调函数 
	    * @param e
	    * 
	    */		
	   private function saveCustomerTypeSuccess(e:ResultEvent):void {			 
			_customerTypeService.removeEventListener(ResultEvent.RESULT, saveCustomerTypeSuccess);
			 if(e.result != null && e.result == "success") {
				AppAlert.show("保存成功！","提示");
				Env.dispatchEvent(new CustomerEvent("saveCustomerTypeSuccess"));
			 } else if(e.result == "ExistSameName"){
			 	//AppAlert.show("存在相同的客户类别名称，请重新输入！","提示");
			 	Env.dispatchEvent(new CustomerEvent("saveCustomerTypeFail",e.result));
			 }else {
			 	//AppAlert.show(e.result as String,"提示");
			 	Env.dispatchEvent(new CustomerEvent("saveCustomerTypeFail",e.result));
			 }				 
		}
		public function checkDelCustomerType(id :int) :void {
			_customerTypeService.addEventListener(ResultEvent.RESULT,checkDelCustomerTypeSuccess);
			_customerTypeService.addEventListener(FaultEvent.FAULT,_Fault);
			_customerTypeService.checkDelCustomerType(id);
		}
		private function checkDelCustomerTypeSuccess(e:ResultEvent):void {
			_customerTypeService.removeEventListener(ResultEvent.RESULT,checkDelCustomerTypeSuccess);
			if(e.result != null) {
				if(e.result == false) {
					AppAlert.show("该客户类别已被使用,不能删除","提示信息");
					return;
				} else {
				 Env.dispatchEvent(new CustomerEvent("checkDelCustomerTypeSuccess",e.result));	
				}
				
			}
		}			
		/**
		 *调用spring方法:删除客户
		 * @param customer
		 * 
		 */		
		public function deleteCustomerType(id:String):void {
				_customerTypeService.addEventListener(FaultEvent.FAULT,_Fault);
				_customerTypeService.addEventListener(ResultEvent.RESULT, deleteCustomerTypeSuccess);
				_customerTypeService.deleteCustomer(id);
		}
	   /**
	    * 删除客户回调函数 
	    * @param e
	    * 
	    */		
	   private function deleteCustomerTypeSuccess(e:ResultEvent):void {			 
			_customerTypeService.removeEventListener(ResultEvent.RESULT, deleteCustomerTypeSuccess);
			 if(e.result != null) {
				AppAlert.show("删除成功！","提示");
				Env.dispatchEvent(new CustomerEvent("deleteCustomerTypeSuccess"));
			 } 
		}
		
	}
}